//
//  LessonPreviewView.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 2/6/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "LessonPreviewView.h"

@interface LessonPreviewView (Private)
#pragma mark UserActions
- (void)tapped;
@end

@implementation LessonPreviewView

@synthesize tapGesture = _tapGesture;
@synthesize delegate;
@synthesize index;
@synthesize saved;

static CGFloat kDefaultReflectionFraction = 0.40;
static CGFloat kDefaultReflectionOpacity = 0.40;

- (id)initWithFrame:(CGRect)frame index:(int)selectedIndex{
    self = [super initWithFrame:frame];
    if (self) {
		self.index = selectedIndex;
		_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
		[self addGestureRecognizer:self.tapGesture];
    }
    return self;
}

- (void)dealloc {
   self.tapGesture = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark UserActions
- (void)tapped{
	[delegate selected:index withImage:[self image]];
}

#pragma mark AsyncImageView Overrides
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection{
	[connection release];
	connection=nil;
	for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	NSData *image = UIImageJPEGRepresentation([UIImage imageWithData:data], 1.0);
	UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:image]] autorelease];
	imageView.contentMode = UIViewContentModeScaleAspectFill;
	[self addSubview:imageView];
   imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2);
   
   NSUInteger reflectionHeight = [self image].size.height * kDefaultReflectionFraction;
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, reflectionHeight)] autorelease];
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.width andHeight:reflectionHeight];
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
	[data release];
	data=nil;
}

- (void)setDisplayImage:(UIImage *)selectedImage{
	for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	UIImageView* imageView = [[[UIImageView alloc] initWithImage:selectedImage] autorelease];
	imageView.contentMode = UIViewContentModeScaleAspectFill;
	[self addSubview:imageView];
	imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2);
	NSUInteger reflectionHeight = [self image].size.height * kDefaultReflectionFraction;
	
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, reflectionHeight)] autorelease];
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.width andHeight:reflectionHeight];
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
}

@end
